﻿using LdtDataEngin.ModuleDetail.BaseModule;
using LdtDataEngin.ModuleDetail.BaseModule.MultiFack;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Model.DataModel.SysCommon;
using LdtJudge.DataEngine.Model.ElmentModel;
using LdtJudge.DataEngine.Model.XMLConfigModel.OptimzeMode;
using LdtJudge.DataEngine.Model.XMLConfigModel.SameNodeConfigModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.CaseCauseElementExtr
{
    public class FactInfoElement : FactElementBaseClass
    {
        #region 基础信息
        /// <summary>
        /// 案由信息
        /// </summary>
        public CaseCauseModel SYS_CaseCause { get; set; }
        /// <summary>
        /// 要素合并配置，条件
        /// </summary>
        public List<SameNodeXml> opconfigList { get; set; }
        /// <summary>
        /// 父节点信息
        /// </summary>
        public CaseCauseModel mSYS_CaseCause { get; set; }
        /// <summary>
        /// 合并后的节点信息
        /// </summary>
        public XmlNode MergeNode { get; set; }

        #endregion

        /// <summary>
        /// 数据处理，合成xml对象
        /// </summary>
        /// <param name="onExtract"></param>
        /// <param name="nodeModelList"></param>
        /// <returns></returns>
        public override XmlNode ExtElementXmlParse(OnExtractComplete onExtract, NodeModelList nodeModelList)
        {
            XmlDocument confirmdocumenttemp = new XmlDocument();
            confirmdocumenttemp.LoadXml(string.Format("<{0}></{0}>", "案由要素"));
            XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
            confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);
            XmlAttribute nameAttr = confirmdocumenttemp.CreateAttribute("案由名称");
            nameAttr.Value = SYS_CaseCause.CC_Name;
            xmlNodeFacttConfirmtemp.Attributes.Append(nameAttr);

            XmlAttribute idAttr = confirmdocumenttemp.CreateAttribute("案由ID");
            idAttr.Value = SYS_CaseCause.CC_ID;
            xmlNodeFacttConfirmtemp.Attributes.Append(idAttr);
            XmlAttribute attribute = confirmdocumenttemp.CreateAttribute("id");
            attribute.Value= Snowflake.Instance().GetId().ToString();
            xmlNodeFacttConfirmtemp.Attributes.Append(attribute);

            CommElementRegexCode commElementRegexCode = new CommElementRegexCode();
            commElementRegexCode.CaseType = _dataContext.DataLayerAns.CaseType;
            commElementRegexCode.SourceCode = _dataContext.DataLayerAns.SourceCode;
            commElementRegexCode.TextType = Convert.ToInt32(_dataContext.DataLayerAns.TextType);
            var groupparsetext = base.GroupParseTextDic.Where(per => !base.suitin.Contains(per.Key.ParagraphCode.ToLower())).ToList();
            ///开始遍历一个人员的事实内容
            foreach (var item in groupparsetext)
            {
                commElementRegexCode.PargarphCode = item.Key.ParagraphCode;
                commElementRegexCode.Purpose = GetExtractTypeBySuitDegreeID(item.Key.SuitDegreeID);
                Dictionary<string, List<string>> ParagraphTemp = new Dictionary<string, List<string>>();
                ParagraphTemp.Add(item.Key.ParagraphCode.ToLower(), new List<string>() { item.Value });
                messagelist = new List<string>();
                ///清空源数据
                base.OriginalTextNodeList.Clear();
                List<NodeModel> listNode = new List<NodeModel>();
                base.LoadeNodeModel(null, mSYS_CaseCause, listNode, item.Value, ParagraphTemp, commElementRegexCode);
                ModelToXml modelToXml = new ModelToXml();
                modelToXml.BuildXmlNode(listNode, SYS_CaseCause);
                if (listNode.Count > 0)
                {
                    ///将提取一次的数据进行存入到提取前数据集合中 
                    foreach (var mode in base.OriginalTextNodeList)
                    {
                        nodeModelList.BeforeMergerListModels.Add((NodeModel)mode.Clone());
                    }
                    nodeModelList.BeforeMergerListModels.AddRange(listNode);
                }
                onExtract(commElementRegexCode.Purpose, listNode, messagelist, item.Key);
                ModelXmlHandle modelXmlHandle = new ModelXmlHandle();
                modelXmlHandle.dataContext = _dataContext;
                XmlNode resultxmlnode = confirmdocumenttemp.Clone();
                xmlNodeFacttConfirmtemp.RemoveAll();
                modelXmlHandle.MergeXmlPro(SYS_CaseCause, listNode, resultxmlnode, opconfigList, xmlNodeFacttConfirmtemp);
            }
            ///生成和合并后的xml;
            return confirmdocumenttemp;  
        }

        ///// <summary>
        ///// 要素提取入口
        ///// </summary>
        ///// <returns></returns>
        //public override List<NodeModel> ExtElementParse(OnExtractComplete onExtract, NodeModelList nodeModelList)
        //{
        //    XmlDocument confirmdocumenttemp = new XmlDocument();
        //    confirmdocumenttemp.LoadXml(string.Format("<{0}></{0}>", SYS_CaseCause.CC_Name));
        //    XmlNode xmlNodeFacttConfirmtemp = confirmdocumenttemp.CreateElement("事实要素");
        //    confirmdocumenttemp.DocumentElement.AppendChild(xmlNodeFacttConfirmtemp);


        //    List<NodeModel> listNodeModel = new List<NodeModel>();
        //    CommElementRegexCode commElementRegexCode = new CommElementRegexCode();
        //    commElementRegexCode.CaseType = _dataContext.DataLayerAns.CaseType;
        //    commElementRegexCode.SourceCode = _dataContext.DataLayerAns.SourceCode;
        //    commElementRegexCode.TextType = Convert.ToInt32(_dataContext.DataLayerAns.TextType);
        //    var groupparsetext = base.GroupParseTextDic.Where(per => !base.suitinfocode.Contains(per.Key.ParagraphCode.ToLower())).ToList();
        //    ///开始遍历一个人员的事实内容
        //    foreach (var item in groupparsetext)
        //    {
        //        commElementRegexCode.PargarphCode = item.Key.ParagraphCode;
        //        commElementRegexCode.Purpose = GetExtractTypeBySuitDegreeID(item.Key.SuitDegreeID);
        //        Dictionary<string, List<string>> ParagraphTemp = new Dictionary<string, List<string>>();
        //        ParagraphTemp.Add(item.Key.ParagraphCode.ToLower(), new List<string>() { item.Value });
        //        messagelist = new List<string>();
        //        ///清空源数据
        //        base.OriginalTextNodeList.Clear();
        //        List<NodeModel> listNode = new List<NodeModel>();
        //        base.LoadeNodeModel(null, mSYS_CaseCause, listNode, item.Value, ParagraphTemp, commElementRegexCode);

        //        if (listNode.Count > 0)
        //        {
        //            ///将提取一次的数据进行存入到提取前数据集合中 
        //            foreach (var mode in base.OriginalTextNodeList)
        //            {
        //                nodeModelList.BeforeMergerListModels.Add((NodeModel)mode.Clone());
        //            }
        //            nodeModelList.BeforeMergerListModels.AddRange(listNode);
        //        }

        //        onExtract(commElementRegexCode.Purpose, listNode, messagelist, item.Key);

        //        ModelXmlHandle modelXmlHandle = new ModelXmlHandle();
        //        XmlNode resultxmlnode = confirmdocumenttemp.Clone();
        //        xmlNodeFacttConfirmtemp.RemoveAll();
        //        modelXmlHandle.MergeXmlPro(SYS_CaseCause, listNode, resultxmlnode, opconfigList, xmlNodeFacttConfirmtemp);
        //    }
        //    ///生成和合并后的xml;
        //    MergeNode = confirmdocumenttemp;
        //    return listNodeModel;
        //}
    }
}
